//
//  BVRegisNextViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/7/1.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVRegisNextViewController.h"
#import "BVCaptchaButton.h"
#import "ZZTextField.h"
#import "BVMineTool.h"

@interface BVRegisNextViewController ()

/***   手机号输入框*/
@property (nonatomic ,strong) UITextField  *telTF;
/***   验证码输入框*/
@property (nonatomic ,strong) UITextField  *verifyCodeTF;
/***   密码输入框*/
@property (nonatomic ,strong) UITextField  *pwdTF;
/***   确认密码输入框*/
@property (nonatomic ,strong) UITextField  *confirmPwdTF;
/***   获取验证码按钮*/
@property (nonatomic ,strong) BVCaptchaButton  *captchaBtn;

@end

@implementation BVRegisNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationItem setTitle:@"注册"];
    [self layoutUI];
}

-(void)layoutUI{
    
    UILabel *telLb = [self labelWithTitle:@"手机号:"];
    telLb.sd_layout
    .topSpaceToView(self.view, 30)
    .leftSpaceToView(self.view, 10)
    .heightIs(40)
    .widthIs(80);
    
    self.telTF = [self textFieldWithPlaceholder:@"请输入您的手机号"];
    [self.telTF setKeyboardType:UIKeyboardTypePhonePad];
    self.telTF.sd_layout
    .topSpaceToView(self.view, 30)
    .leftSpaceToView(telLb, 10)
    .rightSpaceToView(self.view, 10)
    .heightIs(40);
    
    UILabel *verifyCodeLb = [self labelWithTitle:@"验证码:"];
    verifyCodeLb.sd_layout
    .topSpaceToView(telLb, 20)
    .leftSpaceToView(self.view, 10)
    .heightIs(40)
    .widthIs(80);
    
    self.captchaBtn = [[BVCaptchaButton alloc]init];
    [self.view addSubview:self.captchaBtn];
    [self.captchaBtn setCountdown:10];
    [self.captchaBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.captchaBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [self.captchaBtn setBackgroundColor:[UIColor BVBaseOrangeColor]];
    [self.captchaBtn.layer setCornerRadius:5.0];
    [self.captchaBtn addTarget:self action:@selector(fetchCode) forControlEvents:UIControlEventTouchUpInside];
    self.captchaBtn.sd_layout
    .topSpaceToView(self.telTF, 20)
    .rightSpaceToView(self.view, 10)
    .widthIs(100)
    .heightIs(40);
    
    self.verifyCodeTF = [self textFieldWithPlaceholder:@"请输入验证码"];
    [self.verifyCodeTF setKeyboardType:UIKeyboardTypePhonePad];
    self.verifyCodeTF.sd_layout
    .topSpaceToView(self.telTF, 20)
    .leftSpaceToView(verifyCodeLb, 10)
    .rightSpaceToView(self.captchaBtn, 0)
    .heightIs(40);
    
    UILabel *pwdLb = [self labelWithTitle:@"设置密码:"];
    pwdLb.sd_layout
    .topSpaceToView(verifyCodeLb, 20)
    .leftSpaceToView(self.view, 10)
    .heightIs(40)
    .widthIs(80);
    
    self.pwdTF = [self textFieldWithPlaceholder:@"请设置密码"];
    self.pwdTF.sd_layout
    .topSpaceToView(self.verifyCodeTF, 20)
    .leftSpaceToView(pwdLb, 10)
    .rightSpaceToView(self.view, 10)
    .heightIs(40);
    
    UILabel *confirmLb = [self labelWithTitle:@"确认密码:"];
    confirmLb.sd_layout
    .topSpaceToView(pwdLb, 20)
    .leftSpaceToView(self.view, 10)
    .heightIs(40)
    .widthIs(80);
    
    self.confirmPwdTF = [self textFieldWithPlaceholder:@"请确认密码"];
    self.confirmPwdTF.sd_layout
    .topSpaceToView(self.pwdTF, 20)
    .leftSpaceToView(confirmLb, 10)
    .rightSpaceToView(self.view, 10)
    .heightIs(40);
    
    UIButton *regisBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:regisBtn];
    [regisBtn setBackgroundColor:[UIColor BVBaseOrangeColor]];
    [regisBtn setTitle:@"注册相豫" forState:UIControlStateNormal];
    [regisBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [regisBtn addTarget:self action:@selector(regisBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [UIView makeCorner:5.0 view:regisBtn];
    regisBtn.sd_layout
    .topSpaceToView(confirmLb, 30)
    .leftSpaceToView(self.view, 20)
    .rightSpaceToView(self.view, 20)
    .heightIs(40);
    
}

-(void)fetchCode{
    
    [self.hud show:YES];
    [BVMineTool fetchVerifyCode:@"18736972656" success:^(NSString *status) {
        [self.hud hide:YES];
        [self showSuccess:status];
    } failure:^(NSError *error) {
        [self.hud hide:YES];
        [self showError:@"网络错误"];
    }];
}

-(void)regisBtnClick{
    
    if (![NSString valiMobile:self.telTF.text]) {
        [self showWarning:@"手机号格式错误"];
        return;
    }
    
   if (self.verifyCodeTF.text.length <= 0) {
        [self showWarning:@"请输入正确验证码"];
        return;
    }
    
    if (self.pwdTF.text.length <= 0) {
        [self showWarning:@"请输入正确密码"];
        return;
    }
    
    if (![self.confirmPwdTF.text isEqual:self.pwdTF.text]) {
        [self showWarning:@"两次输入密码不一致"];
        return;
    }
    
    [self.hud show:YES];
    [BVMineTool registeredSex:self.sex dateBirth:self.birth address:self.address maritalStatus:self.marital mobile:self.telTF.text code:self.verifyCodeTF.text pwd:self.pwdTF.text success:^(NSString *status) {
         [self.hud hide:YES];
         [self showSuccess:status];
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIViewController *vc = self.navigationController.viewControllers.firstObject;
                ROOTNAVPOPTO(vc, YES);
        });
    } codeFailure:^(NSString *str) {
        
         [self.hud hide:YES];
         [self showSuccess:str];
    } failure:^(NSError *error) {
        
        [self.hud hide:YES];
        [self showError:@"网络错误"];
    }];
    
}

-(UITextField *)textFieldWithPlaceholder:(NSString *)placeholder{
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 45)];
    [imgView setImage:[UIImage imageNamed:@"toiuming"]];
    UITextField *textField = [[UITextField alloc]init];
    [self.view addSubview:textField];
    [textField.layer setBorderColor:[UIColor BVBaseOrangeColor].CGColor];
    [textField.layer setBorderWidth:1.0];
    [textField.layer setCornerRadius:5.0];
    [textField setPlaceholder:placeholder];
    [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    [textField setLeftView:imgView];
    [textField setLeftViewMode:UITextFieldViewModeAlways];
    [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    return textField;
}

-(UILabel *)labelWithTitle:(NSString *)title{
    
    UILabel *label = [[UILabel alloc]init];
    [self.view addSubview:label];
    [label setFont:[UIFont systemFontOfSize:16.0]];
    [label setTextColor:[UIColor BVTextDarkGrayColor]];
    [label setTextAlignment:NSTextAlignmentRight];
    [label setText:title];
    return label;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
